package com.peas.platform.module.icims;

import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.google.zxing.*;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.GenericMultipleBarcodeReader;
import lombok.SneakyThrows;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class QrcodeTestCase {

    private final File file = new File("/Users/caizhaohui/IdeaProjects/icims/data/" + System.currentTimeMillis() + ".jpg");

    @SneakyThrows
    @Test
    public void qrcode() {
        QrCodeUtil.generate("abcdefghijklmnopqrstuvwxyz", 300, 300, file);
        BufferedImage bufferedImage = ImageIO.read(file);
        HashMap<DecodeHintType, String> hints = new HashMap<>(1);
        hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        Result[] results = new GenericMultipleBarcodeReader(multiFormatReader).decodeMultiple(bitmap, hints);
        List<String> resultList = new ArrayList<>(results.length);
        for (Result result : results) {
            resultList.add(result.getText());
        }
        System.out.println(resultList);
    }

}
